package hzn.controller;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import hzn.pojo.User;
import hzn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }
    @Autowired
    UserService service;
    @PostMapping("/regist")
    public String regist(User u, Model m){
        try {
            service.regist(u);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("错误信息："+e.getMessage());
            m.addAttribute("err","注册失败");
        }
        return "redirect:/showLogin";
    }
    @PostMapping("/login")
    public String login(User u, HttpSession session){
        String err = service.login(u);
        if(err==null){
            return "redirect:/showSuccess";
        }else{
            session.setAttribute("err",err);
        }
        return "redirect:/showLogin";
    }
}
